package com.xunge.exception;

import com.xunge.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler
    public Result handleException(Exception e){
        log.error("服务器异常",e);
        return Result.error("服务器异常,请联系管理员");
    }
    @ExceptionHandler
    public Result handleDuplicateKeyException(DuplicateKeyException duplicateKeyException){
        log.error("服务器异常", duplicateKeyException);
        String message = duplicateKeyException.getMessage();
        int i = message.indexOf("Duplicate entry");
        String dke = message.substring(i);
        String[] errMessage = dke.split(" ");

        return Result.error(errMessage[2]+ "已存在");

    }
    @ExceptionHandler
    public Result handleDeleteException(DeleteException deleteException){
        log.error("程序错误", deleteException);
        return Result.error(deleteException.getMessage());
    }

}
